﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using QXGameEngine.Interface;
using QXGameEngine.Logic;

namespace QXGameEngine.Control {

    public partial class QXText : UserControl, IObject {

        /// <summary>
        /// 带阴影的文字控件
        /// </summary>
        public QXText() {
            InitializeComponent();
            Timer = new DispatcherTimer();
            Timer.Interval = TimeSpan.FromMilliseconds(40);
            Timer.Tick += new EventHandler(Timer_Tick);
            Timer.Start();
        }

        /// <summary>
        /// 获取或设置内置计时器
        /// </summary>
        public DispatcherTimer Timer { get; set; }

        /// <summary>
        /// 获取或设置根画布容器
        /// </summary>
        public Canvas RootCanvas {
            get { return this.Container; }
        }

        /// <summary>
        /// 获取父容器
        /// </summary>
        public Canvas ParentCanvas {
            get { return this.Parent as Canvas; }
        }

        /// <summary>
        /// 获取或设置字体尺寸
        /// </summary>
        public double Size {
            get { return Body.FontSize; }
            set { Body.FontSize = value; Shadow.FontSize = value; }
        }

        /// <summary>
        /// 获取或设置文字颜色
        /// </summary>
        public Brush Color {
            get { return Body.Foreground; }
            set { Body.Foreground = value; }
        }

        /// <summary>
        /// 获取或设置字体宽度
        /// </summary>
        public FontWeight Weight {
            get { return Body.FontWeight; }
            set { Body.FontWeight = value; Shadow.FontWeight = value; }
        }

        /// <summary>
        /// 获取或设置字体样式
        /// </summary>
        public FontStyle Fontstyle {
            get { return Body.FontStyle;  }
            set { Body.FontStyle = value; Shadow.FontStyle = value; }
        }

        /// <summary>
        /// 获取或设置文本内容
        /// </summary>
        public string Text {
            get { return Body.Text; }
            set { Body.Text = value; Shadow.Text = value; }
        }

        /// <summary>
        /// 获取或设置文字总宽度
        /// </summary>
        public double TotalWidth {
            get { return Root.Width; }
            set { Root.Width = value; }
        }

        /// <summary>
        /// 获取或设置关键中心点与它左上角X距离值
        /// </summary>
        public double CenterX { get; set; }

        /// <summary>
        /// 获取或设置关键中心点与它左上角Y距离值
        /// </summary>
        public double CenterY { get; set; }

        /// <summary>
        /// 获取或设置文字X坐标(关联属性)
        /// </summary>
        public double X {
            get { return (double)GetValue(XProperty); }
            set { SetValue(XProperty, value); }
        }
        public static readonly DependencyProperty XProperty = DependencyProperty.Register(
            "X",
            typeof(double),
            typeof(QXText),
            new PropertyMetadata(ChangeXProperty)
        );
        private static void ChangeXProperty(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            QXText text = (QXText)d;
            text.SetValue(Canvas.LeftProperty, (double)e.NewValue - text.CenterX);
        }

        /// <summary>
        /// 获取或设置文字Y坐标(关联属性)
        /// </summary>
        public double Y {
            get { return (double)GetValue(YProperty); }
            set { SetValue(YProperty, value); }
        }
        public static readonly DependencyProperty YProperty = DependencyProperty.Register(
            "Y",
            typeof(double),
            typeof(QXText),
            new PropertyMetadata(ChangeYProperty)
        );
        private static void ChangeYProperty(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            QXText obj = (QXText)d;
            obj.SetValue(Canvas.TopProperty, (double)e.NewValue - obj.CenterY);
            obj.SetValue(Canvas.ZIndexProperty, Convert.ToInt32(e.NewValue));
        }

        /// <summary>
        /// 计时器事件
        /// </summary>
        private void Timer_Tick(object sender, EventArgs e) {
            if (this.Opacity <= 0) {
                Timer.Tick -= Timer_Tick;
                Super.RemoveObject(this, true);
            } else {
                this.Opacity -= 0.06;
                this.Y -= 3;
            }
        }
    }
}
